package com.firstsource.networking.ip.tracker.data.entities;

import java.io.Serializable;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Entity;
import org.hibernate.annotations.Formula;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;
import com.firstsource.networking.ip.tracker.util.IPManipulator;

@Entity
@Table(name="subnets")
public class Subnet implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Id
	@Column(name="idx")
	@GeneratedValue(generator="SubnetIndexSequence")
	@GenericGenerator(
				name="SubnetIndexSequence",
				strategy="seqhilo",
				parameters={
						@Parameter(name="max_lo", value="1"),
						@Parameter(name="sequence", value="subnet_idx_seq")
				}
			)
	private int idx = 0 ;

	@Column
	private long network = 0 ;
	
	@Column
	private long netmask = 0 ;

	@Formula(value="(4294967295-netmask)+network")
	private long broadcast = 0 ;
	
	@Column
	private String name ;
	
	@Column
	private String description ;

	@ManyToOne
	@JoinColumn(name="location_id")
	private Location location = null ;

	@OneToMany(mappedBy="subnet")
	private Set<Host> hosts = null ;

	/**
	 * @return the idx
	 */
	public int getIdx() {
		return idx;
	}

	/**
	 * @param idx the idx to set
	 */
	public void setIdx(int idx) {
		this.idx = idx;
	}

	/**
	 * @return the network
	 */
	public String getNetwork() {
		return IPManipulator.long2IP(network) ;
	}

	/**
	 * @param network the network to set
	 */
	public void setNetwork(String network) {
		this.network = IPManipulator.ip2Long(network) ;
	}

	/**
	 * @return the netmask
	 */
	public String getNetmask() {
		return IPManipulator.long2IP(netmask);
	}

	/**
	 * @param netmask the netmask to set
	 */
	public void setNetmask(String netmask) {
		this.netmask = IPManipulator.ip2Long(netmask);
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * @param description the description to set
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	public void setLocation(Location location) {
		this.location = location;
	}

	public Location getLocation() {
		return location;
	}

	public String getBroadcast() {
		return IPManipulator.long2IP(broadcast) ;
	}

	public Set<Host> getHosts() {
		return hosts;
	}
}
